/*     */ package hmc;
/*     */ 
/*     */ import gnu.io.CommPort;
/*     */ import gnu.io.CommPortIdentifier;
/*     */ import gnu.io.NoSuchPortException;
/*     */ import gnu.io.PortInUseException;
/*     */ import gnu.io.SerialPort;
/*     */ import java.io.BufferedWriter;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import javax.swing.JOptionPane;
/*     */ import javax.swing.JTextPane;
/*     */ import javax.swing.text.BadLocationException;
/*     */ import javax.swing.text.Document;
/*     */ import javax.swing.text.Position;
/*     */ import javax.swing.text.StyledDocument;
/*     */ import javax.swing.text.html.HTMLEditorKit;
/*     */ 
/*     */ public class SerialCommunicator
/*     */ {
/*     */   public TopLevelGUI TLGUI;
/*     */   public SerialWriter SW;
/*     */   public SerialReader SR;
/*     */   public Communicator comm;
/*     */   public Thread SRThread;
/*     */   public SerialPort serialPort;
/*     */   public CommPort commPort;
/*     */   public CommPortIdentifier portIdentifier;
/*     */   public BufferedWriter fileOutput;
/*     */   public FileWriter fstream;
/*     */   public File logFile;
/*  66 */   public String command = "";
/*     */   public String compareItem;
/*  68 */   public String portName = "";
/*     */   public int baudrate;
/*     */   public int stopbits;
/*     */   public int databits;
/*     */   public int parity;
/*  76 */   boolean toFile = false;
/*     */ 
/*  78 */   int lineNumber = 0;
/*     */ 
/*     */   public SerialCommunicator(String portName, TopLevelGUI TLGUI)
/*     */   {
/*  82 */     this.portName = portName;
/*  83 */     this.TLGUI = TLGUI;
/*  84 */     this.baudrate = 9600;
/*  85 */     this.stopbits = 1;
/*  86 */     this.databits = 8;
/*  87 */     this.parity = 0;
/*     */   }
/*     */ 
/*     */   public SerialCommunicator(String portName, TopLevelGUI TLGUI, int BR, int DB, int SB, int P) {
/*  91 */     this.portName = portName;
/*  92 */     this.TLGUI = TLGUI;
/*  93 */     this.baudrate = BR;
/*  94 */     this.stopbits = SB;
/*  95 */     this.databits = DB;
/*  96 */     this.parity = P;
/*     */   }
/*     */ 
/*     */   public void sendCommand(String x) {
/* 100 */     this.command = (x + "\r");
/*     */     try
/*     */     {
/* 103 */       this.SW.send();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */     }
/*     */ 
/* 109 */     this.command = "";
/*     */   }
/*     */ 
/*     */   public void simpleSendCommand(String x) {
/* 113 */     this.command = x.replace("\n", "\r");
/*     */     try
/*     */     {
/* 116 */       this.SW.send();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */     }
/*     */ 
/* 122 */     this.command = "";
/*     */   }
/*     */ 
/*     */   public void closeStreams()
/*     */   {
/*     */     try {
/* 128 */       if (this.SR.isConnected())
/* 129 */         this.comm.JavaPrinting("\nDisconnected.\n");
/* 130 */       this.SW.closeStream();
/* 131 */       this.SR.closeStream();
/* 132 */       this.serialPort.close();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */     }
/*     */   }
/*     */ 
/*     */   public void connect()
/*     */   {
/*     */     try
/*     */     {
/* 143 */       System.out.println("SERIAL COMMUNICATOR  " + this.portName);
/* 144 */       this.portIdentifier = CommPortIdentifier.getPortIdentifier(this.portName);
/*     */ 
/* 147 */       this.commPort = this.portIdentifier.open(getClass().getName(), 10000);
/*     */ 
/* 149 */       this.serialPort = ((SerialPort)this.commPort);
/* 150 */       this.serialPort.setSerialPortParams(this.baudrate, this.databits, this.stopbits, this.parity);
/*     */ 
/* 152 */       InputStream in = this.serialPort.getInputStream();
/* 153 */       OutputStream out = this.serialPort.getOutputStream();
/*     */ 
/* 155 */       this.SR = new SerialReader(in, this.comm);
/* 156 */       this.SRThread = new Thread(this.SR);
/* 157 */       this.SRThread.setPriority(10);
/* 158 */       this.SRThread.start();
/*     */ 
/* 160 */       this.SW = new SerialWriter(out, this);
/* 161 */       new Thread(this.SW).start();
/*     */ 
/* 163 */       this.comm.JavaPrinting(" \nConnected To: " + this.portName + "\n ");
/* 164 */       this.comm.ConsoleBox.setCaretPosition(this.comm.ConsoleBox.getDocument().getLength());
/*     */     }
/*     */     catch (PortInUseException piue)
/*     */     {
/* 168 */       JOptionPane.showMessageDialog(null, "Port is in use.", "ERROR", 0);
/*     */     }
/*     */     catch (NoSuchPortException nspe)
/*     */     {
/* 172 */       JOptionPane.showMessageDialog(null, "Port does not exist.", "ERROR", 0);
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 176 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public void setSPC(Communicator x) {
/* 181 */     this.comm = x;
/*     */   }
/*     */ 
/*     */   public void writeLogFile(File logFile, boolean HTMLOutput, int dataLogStartLine)
/*     */   {
/*     */     try {
/* 187 */       this.fstream = new FileWriter(logFile);
/* 188 */       this.fileOutput = new BufferedWriter(this.fstream);
/* 189 */       SimpleDateFormat custDateFormat = new SimpleDateFormat("EEE MMM-dd HH:mm:ss.SSS zzz yyyy");
/* 190 */       Date date = new Date();
/* 191 */       this.fileOutput.write("[" + custDateFormat.format(date) + "]\n");
/*     */ 
/* 193 */       if (!HTMLOutput)
/*     */       {
/* 195 */         String text = this.comm.ConsoleBox.getText();
/* 196 */         String[] temp = text.split("\n");
/*     */ 
/* 198 */         for (int x = dataLogStartLine; x < temp.length; x++)
/*     */         {
/* 200 */           this.fileOutput.write(text);
/*     */         }
/*     */       }
/*     */       else
/*     */       {
/* 205 */         StyledDocument doc = this.comm.ConsoleBox.getStyledDocument();
/*     */ 
/* 207 */         HTMLEditorKit kit = new HTMLEditorKit();
/*     */         try
/*     */         {
/* 211 */           kit.write(this.fileOutput, doc, doc.getStartPosition().getOffset(), doc.getLength());
/*     */         }
/*     */         catch (FileNotFoundException e)
/*     */         {
/*     */         }
/*     */         catch (IOException e)
/*     */         {
/*     */         }
/*     */         catch (BadLocationException e)
/*     */         {
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/* 227 */       this.fileOutput.close();
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 231 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public class SerialWriter
/*     */     implements Runnable
/*     */   {
/*     */     OutputStream out;
/*     */     SerialCommunicator ST;
/*     */ 
/*     */     public SerialWriter(OutputStream out, SerialCommunicator x)
/*     */     {
/* 325 */       this.out = out;
/* 326 */       this.ST = x;
/*     */     }
/*     */ 
/*     */     public void run()
/*     */     {
/*     */     }
/*     */ 
/*     */     public void send() {
/* 334 */       byte[] c = SerialCommunicator.this.command.getBytes();
/*     */ 
/* 337 */       for (int x = 0; x < c.length; x++)
/*     */       {
/*     */         try
/*     */         {
/* 341 */           this.out.write(c[x]);
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/*     */     public void closeStream()
/*     */     {
/*     */       try
/*     */       {
/* 353 */         if (SerialCommunicator.this.fileOutput != null) {
/* 354 */           SerialCommunicator.this.fileOutput.close();
/*     */         }
/* 356 */         this.out.close();
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public class SerialReader
/*     */     implements Runnable
/*     */   {
/*     */     private InputStream in;
/* 237 */     public int howLong = 1000;
/* 238 */     public boolean running = true;
/*     */     Communicator comm;
/*     */ 
/*     */     public SerialReader(InputStream in, Communicator x)
/*     */     {
/* 243 */       this.in = in;
/* 244 */       this.comm = x;
/*     */     }
/*     */ 
/*     */     public void run() {
/* 248 */       while (this.running)
/*     */       {
/*     */         try
/*     */         {
/* 252 */           boolean backspaced = false;
/* 253 */           int len = 0;
/*     */ 
/* 255 */           int da = -1;
/*     */ 
/* 257 */           da = this.in.read();
/*     */           String dat;
/*     */           String dat;
/* 261 */           if (da == -1)
/*     */           {
/* 263 */             dat = "";
/*     */           }
/*     */           else {
/* 266 */             dat = Character.toString((char)da);
/*     */           }
/* 268 */           if ((dat.length() != 0) && (dat.codePointAt(0) == 8))
/*     */           {
/* 270 */             backspaced = true;
/* 271 */             StyledDocument doc = this.comm.ConsoleBox.getStyledDocument();
/* 272 */             if (doc != null)
/*     */               try {
/* 274 */                 doc.remove(doc.getLength() - 1, 1);
/* 275 */                 this.comm.ConsoleBox.setStyledDocument(doc);
/* 276 */                 this.comm.ConsoleBox.setCaretPosition(this.comm.ConsoleBox.getDocument().getLength());
/*     */               }
/*     */               catch (Exception e)
/*     */               {
/*     */               }
/*     */           }
/* 282 */           if (!backspaced)
/*     */           {
/* 284 */             int pos = this.comm.ConsoleBox.getDocument().getLength();
/*     */ 
/* 286 */             if (!dat.equals("")) {
/* 287 */               this.comm.append(dat);
/*     */             }
/* 289 */             if (pos != this.comm.ConsoleBox.getDocument().getLength())
/* 290 */               this.comm.ConsoleBox.setCaretPosition(this.comm.ConsoleBox.getDocument().getLength());
/*     */           }
/* 292 */           backspaced = false;
/*     */         }
/*     */         catch (Exception e)
/*     */         {
/* 296 */           System.out.println("serial read failed");
/* 297 */           e.printStackTrace();
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/*     */     public void closeStream()
/*     */     {
/*     */       try {
/* 305 */         this.running = false;
/* 306 */         this.in.close();
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/*     */       }
/*     */     }
/*     */ 
/*     */     public boolean isConnected()
/*     */     {
/* 315 */       return this.running;
/*     */     }
/*     */   }
/*     */ }

/* Location:           F:\6-30-2012\Hazell\
 * Qualified Name:     hmc.SerialCommunicator
 * JD-Core Version:    0.6.0
 */